package com.welab.pandahelper.http;

import com.google.gson.internal.$Gson$Types;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import okhttp3.Call;
import okhttp3.Response;

public abstract class IBaseCallBack<T> {

    public Type mType;
    public IBaseCallBack() {
        mType = getSuperclassTypeParameter(getClass());
    }

    /**
     * 将Type类型转换成Gson，解析
     */
    static Type getSuperclassTypeParameter(Class<?> subclass) {
        Type superclass = subclass.getGenericSuperclass();
        if (superclass instanceof Class) {
            throw new RuntimeException("Missing type parameter.");
        }
        ParameterizedType parameterized = (ParameterizedType) superclass;
        return $Gson$Types.canonicalize(parameterized.getActualTypeArguments()[0]);
    }

    //请求成功前回调
    public abstract void onRequestBefore();

    //网络请求成功后根据返回码调用
    public abstract void onSuccess(Response response, T t);

    //请求失败时
    public abstract void onFailure(Call call, Exception e);
}